package com.shaoyu.spring.study.dto;


import lombok.Data;

/**
 * @Description:
 * @author: shaoyu1
 * @date: 2020/11/5 14:03:31
 */
@Data
public class BaseResponse<R> extends BasicData{

    private int code;

    private String errorMsg;

    public boolean isSuccess(){
        return code == 0;
    }

    private R response;

    public static <R> BaseResponse<R> build(R response){
        BaseResponse<R> baseResponse = new BaseResponse<>();
        baseResponse.response = response;
        return baseResponse;
    }
    @Override
    public <T> T get(String filedName){
        try{
            if(response==null){
                return super.get(filedName);
            }
            T value = super.get(filedName,this.response);
            return value != null ? value : super.get(filedName);
        }catch (Exception e){
            return null;
        }
    }
    @Override
    public boolean set(String filedName, Object property){
        try{
            return response==null?super.set(filedName, property):super.set(filedName,response,property)||super.set(filedName, property);
        }catch (Exception e){
            return false;
        }
    }
}
